package com.mxprojects.gui
{
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.Utils;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.events.TextEvent;
	import flash.text.TextFieldAutoSize;
	
	public class PageNav
	{
		private static const PAGES_BEFORE_AND_AFTER:int = 2;
		private static const SHOW_PREV_NEXT_BUTTONS:Boolean = false;
		private static const NEXT_BUTTON:String = "»";
		private static const PREV_BUTTON:String = "«";
		
		private static var pageNav:SimpleTextField;
		
		public function PageNav(){}
		
		public static function setup():void {
			//just sets up the text block to hold the page data
			pageNav = new SimpleTextField("");
			pageNav.y = Styles.css.getStyle(".pagesBase").y;
			pageNav.autoSize = TextFieldAutoSize.RIGHT;
			MXP3.header.addChild(pageNav);
		}
		
		//static function to update the page nav in the header_mc
		public static function createNav(currentPage:int, totalPages:int, locationID:int = -1, ...extraStuff:Array):String {
			var pages:String = "";
			var startPage:int;
			var endPage:int;
			var section:String = MXP3.currentLocation;
			if(extraStuff[1] == Word.PRIVATE_MESSAGES){
				section = extraStuff[0];
			}
			
			//Will look like "Pages: (15) 1 ... 5 6 7 8 9 ... 15"
			//If buttons are TRUE "Pages: (15) 1 ... « 5 6 7 8 9 » ... 15"
			//pages = "<span class=\"pagesBase\"><span class=\"pagesTitle\">"+ Language.main(Word.PAGES) +":</span> (" + Utils.numberFormat(totalPages) + ") ";
			pages = "<span class=\"pagesBase\"><span class=\"pagesTitle\">"+ Language.main(Word.PAGES) +":</span>";
		
			//Determine start page (page 1 or two pages before the current page, whichever is higher)
			startPage = Math.max(1, currentPage - PAGES_BEFORE_AND_AFTER);
		
			//Determine end page (last page or two pages past the current page, whichever is the lower).
			endPage = Math.min(totalPages, currentPage + PAGES_BEFORE_AND_AFTER);
			
			//If start page is greater than 1 (means page 1 will be floating)
			if(startPage > 1) {
				//Show the 1 ... so you can jump to page 1
				pages += "<a class=\"pagesLink\" href=\"event:" + section + "," + locationID + ",1," + extraStuff + "\">1</a>  ...";		
			}
		
			//If not page 1 and we are showing buttons
			if(SHOW_PREV_NEXT_BUTTONS){
				if (currentPage > 1) {
					//Adds the previous button
					pages += "<a href=\"event:" + section + "," + locationID + "," + (currentPage - 1) + "," + extraStuff + "\"> <u>"+PREV_BUTTON+"</u></a>";		
				}
			}
			
			//Do main page links, from startPath up to (and including) endPage
			for (var i:int = startPage; i <= endPage; i++) {
				if (currentPage == i) {
					//Just show the page number
					pages += "  <span class=\"pagesCurrentPage\">" + Utils.numberFormat(i) + "</span>";
				} else {
					//Otherwise make the page number clickable
					pages += "  <a class=\"pagesLink\" href=\"event:" + section + "," + locationID + "," + i + "," + extraStuff + "\">" + Utils.numberFormat(i) + "</a>";
				}		
			}
			
			//If it's not the last page and we are showing buttons
			if(SHOW_PREV_NEXT_BUTTONS){
				if(currentPage < totalPages) {
					//Adds the next button
					pages += "<a href=\"event:"+ section + "," + locationID + "," + (currentPage + 1) + "," + extraStuff + "\"> <u>"+NEXT_BUTTON+"</u></a>";
				}
			}
			
			//If the last page is floating (greater than current page + PAGES_BEFORE_AND_AFTER
			if(endPage < totalPages) {
				//Show the last page number to jump to that page
				pages += "  ...  <a class=\"pagesLink\" href=\"event:" + section + "," + locationID + "," + totalPages + "," + extraStuff + "\">"+ Utils.numberFormat(totalPages) +"</a>";		
			}
			
			//finish this bad boy off
			pages += "</span>";
			
			//set the text!
			if(extraStuff[1] != Word.PRIVATE_MESSAGES){
				pageNav.htmlText = pages;
				pageNav.x = Math.round(MXP3.wStage - pageNav.width - (Config.TEXT_PADDING/2));
				pageNav.addEventListener(TextEvent.LINK, loadLink);
			}
			
			return pages;
		}
		
		public static function clearNav():void {
			if(pageNav != null){
				pageNav.htmlText = "";
				pageNav.removeEventListener(TextEvent.LINK, loadLink);
			}
		}
		
		public static function resize():void {
			if(pageNav != null){
				pageNav.x = Math.round(MXP3.wStage - pageNav.width);
			}
		}
		
		public static function get navText():SimpleTextField {
			return pageNav;
		}
		
		private static function loadLink(event:TextEvent):void {
			//split up the functions comes in like "Threads.load,1,3" <-- load the threads from ForumID 1 and Page 3
			var chunks:Array = event.text.split(",");

			//let's break up the page numbers from the IDs
			var pageInfo:Array = chunks.splice(1);
			
			//if we have extra info, then we need to push that into the pageInfo array as well
			DeepLinking.setLink(chunks[0], pageInfo);
		}

	}
}